package com.asj.vivo.lock.service.binding;


import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;

import com.google.gson.JsonObject;

import java.io.File;

/**
 * 绑定服务之一：获取设备信息服务
 * */
public class BindingGetDeviceInfoService {
    @SuppressLint("StaticFieldLeak")
    private static Context context;
    @SuppressLint("StaticFieldLeak")
    private static BindingGetDeviceInfoService deviceInfoService;

    public static synchronized BindingGetDeviceInfoService getInstance(Context context){
        return new BindingGetDeviceInfoService(context);
    }
    public BindingGetDeviceInfoService(Context context){
        BindingGetDeviceInfoService.context = context;
    }

    /**
     * 获取设备型号
     * */
    public JsonObject getDeviceModel(JsonObject jsonObject){
        String deviceModel = Build.MODEL;
        String deviceManufacturer = Build.MANUFACTURER;

        String os = "Android";
        String osVersion = Build.VERSION.RELEASE;

        //设备型号
        jsonObject.addProperty("deviceModel",deviceModel);
        //设备制造商
        jsonObject.addProperty("deviceManufacturer",deviceManufacturer);
        //操作系统
        jsonObject.addProperty("os",os);
        //操作系统版本
        jsonObject.addProperty("osVersion",osVersion);
        //设备识别码
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        //运行商网络
        String operatorName = telephonyManager.getNetworkOperatorName();
        jsonObject.addProperty("operatorName",operatorName);
        //运行内存容量
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        long totalMem = mi.totalMem;
        double ramInGB = (double) totalMem / (1024 * 1024 * 1024);
        jsonObject.addProperty("ramInGB",ramInGB);

        //内部存储容量
        File dataDir = Environment.getDataDirectory();
        StatFs statFs = new StatFs(dataDir.getPath());
        long totalBytes = statFs.getTotalBytes();
        double internalStorageInGB = ((double) totalBytes / (1024 * 1024 * 1024));
        jsonObject.addProperty("internalStorageInGB",internalStorageInGB);

        //系统内核版本号
        String kernelVersion = Build.VERSION.CODENAME;
        jsonObject.addProperty("kernelVersion",kernelVersion);
        //系统安全补丁程序级别
        String securityPatch = Build.VERSION.SECURITY_PATCH;
        jsonObject.addProperty("securityPatch",securityPatch);
        //CPU型号
        String cpuAbi = Build.CPU_ABI;
        jsonObject.addProperty("cpuAbi",cpuAbi);
        return jsonObject;
    }



    /**
     * 获取电池电量
     * */
    public  String getBatteryPercentage(){
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, intentFilter);
        assert batteryStatus != null;
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        float batteryPct = level / (float) scale;
        // 将电池电量转换为百分比
        return String.valueOf(batteryPct * 100);
    }

    /**
     * 获取电池状态
     * */
    public  String getBatteryStatus(){
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, intentFilter);
        assert batteryStatus != null;
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        float batteryPct = level / (float) scale;
        // 将电池电量转换为百分比
        return String.valueOf(batteryPct * 100);
    }

    /**
     * 获取网络类型（2wifi,1流量）
     * */
    public  int getWifiOrFlow() {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            int networkType = activeNetworkInfo.getType();
            if (networkType == ConnectivityManager.TYPE_WIFI) {
                return 2;//wifi
            } else if (networkType == ConnectivityManager.TYPE_MOBILE) {
                return 1;//流量
            }
        }
        return 0; //无网络连接
    }
}
